package vn.hocweb.dao.dao_implement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vn.hocweb.dao.dao_interface.IViDuDAO;
import vn.hocweb.entity.ViDu;

public class ViDuDAO extends GenericDAO implements IViDuDAO{

	@Override
	public ViDu findByID(Long id) {
		Connection connection = getConnection();
		ViDu result = null;
		String sql = "select * from vidu where mavidu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, id);
			ResultSet effected = pre.executeQuery();
			if (effected.next()){
				long maViDu = effected.getLong("mavidu");
				
				String ten = effected.getString("ten");
				String maNguon = effected.getString("manguon");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maBaiViet = effected.getLong("mabaiviet");
				
				result = new ViDu(thuTu, noiBat, hienThi, maViDu,
						ten, maNguon, maBaiViet);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<ViDu> findAll(int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<ViDu> result = new ArrayList<ViDu>();
		String sql = "select * from vidu limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, firstItem);
			pre.setLong(2, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maViDu = effected.getLong("mavidu");
				
				String ten = effected.getString("ten");
				String maNguon = effected.getString("manguon");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maBaiViet = effected.getLong("mabaiviet");
				
				ViDu row = new ViDu(thuTu, noiBat, hienThi, maViDu,
						ten, maNguon, maBaiViet);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public int size() {
		Connection connection = getConnection();
		int result = 0;
		String sql = "select count(*) from vidu";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			ResultSet effected = pre.executeQuery();
			if(effected.next()){
				result = effected.getInt(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean save(ViDu entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "insert into vidu (ten, manguon, thutu, noibat, hienthi, mabaiviet) " +
				"values (?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getTen());
			pre.setString(2, entity.getMaNguon());
			pre.setInt(3, entity.getThuTu());
			pre.setBoolean(4, entity.isNoiBat());
			pre.setBoolean(5, entity.isHienThi());
			pre.setLong(6, entity.getBaiViet().getMaBaiViet());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean delete(ViDu entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "delete from vidu where mavidu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, entity.getMaViDu());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean remove(ViDu entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update vidu set hienthi = ? where mavidu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, false);
			pre.setLong(2, entity.getMaViDu());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean restore(ViDu entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update vidu set hienthi = ? where mavidu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, true);
			pre.setLong(2, entity.getMaViDu());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<ViDu> findAll_danhSachViDu(long maBV, int firstItem,
			int batchSize) {
		Connection connection = getConnection();
		List<ViDu> result = new ArrayList<ViDu>();
		String sql = "select * from vidu where mabaiviet = ? limit ?, ?";
		PreparedStatement pre;
		try {
			pre = connection.prepareStatement(sql);
			pre.setLong(1, maBV);
			pre.setInt(2, firstItem);
			pre.setInt(3, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maViDu = effected.getLong("mavidu");
				
				String ten = effected.getString("ten");
				String maNguon = effected.getString("manguon");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maBaiViet = effected.getLong("mabaiviet");
				
				ViDu row = new ViDu(thuTu, noiBat, hienThi, maViDu,
						ten, maNguon, maBaiViet);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

}
